package xin.marcher.wind.process.controller.admin;

import org.springframework.web.bind.annotation.*;
import xin.marcher.framework.common.util.validation.ParamCheckUtil;
import xin.marcher.framework.common.web.response.BaseResult;
import xin.marcher.wind.process.domain.dto.ProcessNodeDTO;
import xin.marcher.wind.process.domain.dto.ProcessNodeListDTO;
import xin.marcher.wind.process.domain.request.DeleteProcessNodeRequest;
import xin.marcher.wind.process.domain.request.ProcessNodeQuery;
import xin.marcher.wind.process.domain.request.SaveProcessNodeRequest;
import xin.marcher.wind.process.enums.ProcessErrorCodeEnum;
import xin.marcher.wind.process.enums.ProcessTypeEnum;
import xin.marcher.wind.process.service.ProcessNodeService;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 流程节点 前端控制器
 * </p>
 *
 */
@RestController
@RequestMapping("/process-node")
public class ProcessNodeController {

    @Resource
    private ProcessNodeService processNodeService;

    /**
     * 保存流程节点
     * @param request 请求
     * @return 响应
     */
    @PostMapping("/")
    public BaseResult<Boolean> save(@RequestBody SaveProcessNodeRequest request) {

        // 1、校验入参
        ParamCheckUtil.checkIntAllowableValues(request.getType(), ProcessTypeEnum.allowableValues(),"type");
        ParamCheckUtil.checkStringNonEmpty(request.getName(), ProcessErrorCodeEnum.PROCESS_NODE_NAME_IS_EMPTY);
        ParamCheckUtil.checkStringNonEmpty(request.getBeanName(), ProcessErrorCodeEnum.PROCESS_NODE_BEAN_NAME_IS_EMPTY);
        ParamCheckUtil.checkStringNonEmpty(request.getBeanClazzName(), ProcessErrorCodeEnum.PROCESS_NODE_BEAN_CLAZZ_NAME_IS_EMPTY);

        // 2、保存
        processNodeService.save(request);

        return BaseResult.success();
    }

    /**
     * 删除流程节点
     * @param request 请求
     * @return 响应
     */
    @PostMapping("/delete")
    public BaseResult<Boolean> delete(@RequestBody DeleteProcessNodeRequest request) {

        // 1、校验入参
        ParamCheckUtil.checkStringNonEmpty(request.getName(), ProcessErrorCodeEnum.PROCESS_NODE_NAME_IS_EMPTY);

        // 2、删除
        processNodeService.delete(request.getName());

        return BaseResult.success();
    }

    /**
     * 流程节点详情
     * @param name 流程节点名称
     * @return 响应
     */
    @GetMapping("/detail")
    public BaseResult<ProcessNodeDTO> detail(String name) {

        // 1、校验入参
        ParamCheckUtil.checkStringNonEmpty(name, ProcessErrorCodeEnum.PROCESS_NODE_NAME_IS_EMPTY);

        // 2、详情
        return BaseResult.success(processNodeService.detail(name));
    }

    /**
     * 流程节点列表查询
     * @param query 查询请求
     * @return 响应
     */
    @PostMapping("/list")
    public BaseResult<List<ProcessNodeListDTO>> list(@RequestBody ProcessNodeQuery query) {
        return BaseResult.success(processNodeService.list(query));
    }

}
